/**
 * User: Oleksii Savchenko
 * Date: 22.02.13
 * Time: 10:11
 */
window.onload = function(){
    mainActions.getItems();

    /**
     * Close delete popup
     * @type {NodeList}
     */
    var closePopupButtons = document.querySelectorAll('.win-close');
    _.each(closePopupButtons, function(button){
        button.addEventListener('click', function(eve){
            eve.preventDefault();
            mainActions.closePopup('#removeItem');
        })
    });

    /**
     * Call action delete item
     */
    document.querySelector('.act-remove').addEventListener('click', function(eve){
        eve.preventDefault();
        mainActions.removeItem();
    });

    /**
     * Open popup Add new Item
     */
    document.querySelector('.add-new').addEventListener('click', function(eve){
        eve.preventDefault();
        mainActions.openPopup('#addItem');
    });

    /**
     * Close Add form
     * @type {NodeList}
     */
    var closeFormButtons = document.querySelectorAll('.close-form');
    _.each(closeFormButtons, function(button) {
        button.addEventListener('click', function(eve){
            eve.preventDefault();
            mainActions.closePopup('#addItem');
        })
    });

    /**
     * Call action Add item
     */
    document.querySelector('.add-item').addEventListener('click', function(eve){
        eve.preventDefault();
        mainActions.addItem('#addForm');
    });

    /**
     * Call action Sort By
     */
    document.querySelector('#inputSortBy').addEventListener('change', function(){
        mainActions.sortBy(this);
    });

    /**
     * Handle events
     */
    document.querySelector('#text-area').addEventListener('click', function(eve){
        event = event || window.event;
        var target = event.target || event.srcElement;

        if(target.getAttribute('data-elem') === 'textField'){
            target.addEventListener('change', function(){
                mainActions.showChangeIcon(this);
            });
        }
        if(target.getAttribute('data-elem') === 'buttonEdit'){
            target.addEventListener('click', function(eve){
                eve.preventDefault();
                mainActions.editItem(this);
            });
        }
        if(target.getAttribute('data-elem') === 'buttonOpenWind'){
            target.addEventListener('click', function(eve){
                eve.preventDefault();
                mainActions.openPopup('#removeItem', this);
            });
        }
    });

};